package cn.com.codingce.student.servlet;

import cn.com.codingce.student.entity.Login;
import cn.com.codingce.student.entity.Page;
import cn.com.codingce.student.entity.Student;
import cn.com.codingce.student.service.IStudentService;
import cn.com.codingce.student.service.ITeacherService;
import cn.com.codingce.student.service.impl.StudentServiceImpl;
import cn.com.codingce.student.service.impl.TeacherServiceImpl;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * Servlet implementation class QueryStudentByPage
 */
@WebServlet("/QueryStudentByPage")
public class QueryStudentByPage extends HttpServlet {
	private static final long serialVersionUID = 1L;
       



	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		/*
		 * 先接口在实现类  学生
		 */
		IStudentService studentService = new StudentServiceImpl();
		ITeacherService itea = new TeacherServiceImpl();
		
		
		int count = studentService.getTotalCount();//查询总数
		
		
		String cPage = request.getParameter("currentPage");
		if(cPage == null){
			cPage = "1";
		}
		
		
		
		//五个字段  (其中一个自动计算)  四个    组装page对象(自己的)
		Page page = new Page();
		
		
		
		//第四个    总数据数     放到最上面  注意顺序
		int totalCount = studentService.getTotalCount();//查询总数
		page.setTotalCount(totalCount);
		
	
		
		
		//第一个
		int currentPage = Integer.parseInt(cPage);
		page.setCurrentPage(currentPage);
		
		
		
		//第二个
		int pageSize = 3;
		page.setPageSize(pageSize);
		
		//第三个
		List<Student> students = studentService.queryStudentByPage(currentPage, pageSize);
		page.setStudents(students);
		
		
		
		//System.out.println(page.getTotalPage());
		/*
		 *教师
		 */
		Login login = new Login();
		List<Login> teachers = itea.queryAllTeachers();
		request.setAttribute("teachers", teachers);
		
		
		
		
		request.setAttribute("p",page);
		request.getSession().setAttribute("stuCount",totalCount);
		
		System.out.println(teachers);
		int teaCount = itea.getTotalCount();
	
		request.getSession().setAttribute("teaCount",teaCount);
		
		request.getRequestDispatcher("index.jsp").forward(request, response);
		
	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		doGet(request, response);
	}

}
